using System;
using System.Data;
using HRM.Common;
namespace HRM.BusinessLogic
{
    public class GradeInfo : Standard_Info
    {

        public GradeInfo()
        {
        }


        private Int32 _Grade_Id;
        public Int32 Grade_Id
        {
            get { return _Grade_Id; }
            set { _Grade_Id = value; }
        }

        private String _Grade_Name;
        public String Grade_Name
        {
            get { return _Grade_Name; }
            set { _Grade_Name = value; }
        }

        private String _Grade_Branchcode;
        public String Grade_Branchcode
        {
            get { return _Grade_Branchcode; }
            set { _Grade_Branchcode = value; }
        }
    }

    public class GradeDetailInfo : Standard_Info
    {

        public GradeDetailInfo()
        {
        }


        private Int32 _GradeDetail_Id;
        public Int32 GradeDetail_Id
        {
            get { return _GradeDetail_Id; }
            set { _GradeDetail_Id = value; }
        }

        private string _GradeDetail_BranchCode;

        public string GradeDetail_BranchCode
        {
            get { return _GradeDetail_BranchCode; }
            set { _GradeDetail_BranchCode = value; }
        }
        private int _GradeDetail_GradeId;

        public int GradeDetail_GradeId
        {
            get { return _GradeDetail_GradeId; }
            set { _GradeDetail_GradeId = value; }
        }
        private int _GradeDetail_LevelId;

        public int GradeDetail_LevelId
        {
            get { return _GradeDetail_LevelId; }
            set { _GradeDetail_LevelId = value; }
        }
        private string _GradeDetail_LevelName;

        public string GradeDetail_LevelName
        {
            get { return _GradeDetail_LevelName; }
            set { _GradeDetail_LevelName = value; }
        }
        private decimal _GradeDetail_Amount;

        public decimal GradeDetail_Amount
        {
            get { return _GradeDetail_Amount; }
            set { _GradeDetail_Amount = value; }
        }
        private int _GradeDetail_Month;

        public int GradeDetail_Month
        {
            get { return _GradeDetail_Month; }
            set { _GradeDetail_Month = value; }
        }

    }

    public class Grade_Controller : Standard_Controller
    {
        #region "Contructor"
        private static Grade_Controller _instance;
        protected Grade_Controller() { }
        protected new void Dispose()
        {
            _instance = null;
        }
        private static object _syncLock = new object();
        public static Grade_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new Grade_Controller();
                }
            }
            return _instance;
        }
        public static Grade_Controller Instance(String transName)
        {
            if (!String.IsNullOrEmpty(transName))
            {
                Grade_Controller _instanceTrans = new Grade_Controller();
                _instanceTrans.Trans_Name = transName;
                return _instanceTrans;
            }
            else
                return Instance();
        }
        #endregion

        public const String cs_Table_Name = "HRM_Grade";

        public int Grade_Using(int GradeID)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_Using",false), GradeID));
        }

        public DataSet Grade_GetAll()
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_GetAll", false));
        }

        //public DataTable Grade_GetAll()
        //{
        //    return DataConnect.ExecuteTable(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_GetAll", false));
        //}

        public DataSet Grade_GetList(out int ItemCount, string branchcode, int currentPage, int pageSize)
        {
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_GetList", false), branchcode, currentPage, pageSize);
            if (ds != null && ds.Tables.Count > 0)
                ItemCount = Globals.Object_GetValue<int>(ds.Tables[1].Rows[0][0]);
            return ds;
        }

        public DataSet Grade_GetByBranch(int BranchID)
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_GetByBranch",false), BranchID);
        }

        public DataSet Grade_GetWithPosition(int GradeID)
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_GetWithPosition", false), GradeID);
        }

        public GradeInfo Grade_GetInfo(int GradeID)
        {
            return CBO.FillObject<GradeInfo>(DataConnect.ExecuteRow(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_GetInfo", false), GradeID));
        }

        public int Grade_Insert(GradeInfo eInfo)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_Insert", false), eInfo.Grade_Name, eInfo.Grade_Branchcode));
        }

        public bool Grade_Update(GradeInfo eInfo)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_Update", false), eInfo.Grade_Id, eInfo.Grade_Name, eInfo.Grade_Branchcode));
        }

        public bool Grade_Delete(int GradeID)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Grade_Delete", false), GradeID));
        }


        public void InsertTable(DataTable dtData)
        {
            using (DataTable dt = dtData.GetChanges(DataRowState.Deleted))
            {
                if(dt!=null)
                    foreach (DataRow dr in dt.GetChanges(DataRowState.Deleted).Rows)
                    {
                        Grade_Delete(Globals.DB_GetValue<int>(dr["Grade_ID", DataRowVersion.Original]));
                    }
            }
            
            //
            dtData.AcceptChanges();
            //
            GradeInfo objInfo;
            foreach (DataRow dr in dtData.Rows)
            {
                objInfo = new GradeInfo();
                //
                Globals.DataRow_FillObject(dr, objInfo);
                if (objInfo.Grade_Id > 0)
                    Grade_Update(objInfo);
                else
                {
                   dr["Grade_ID"] =  Grade_Insert(objInfo);
                }
            }
        }

        public void Clear(string sBranch_Code)
        {
            
        }

        public DataTable Grade_ListById(int Id)
        {
            string sQuery = "SELECT * FROM vw_HRM_GradeDetail WHERE GradeDetail_GradeID = " + Id.ToString();
            return DataConnect.ExecuteTable(DataConnect.WorkingConnect, CommandType.Text, sQuery);
        }

        public DataTable Grade_ListByBranch(string strBranchCode)
        {
            string sQuery = "SELECT * FROM vw_HRM_GradeDetail WHERE GradeDetail_BranchCode = '" + strBranchCode + "'";
            return DataConnect.ExecuteTable(DataConnect.WorkingConnect, CommandType.Text, sQuery);
        }

        public void SaveDetail(DataTable dtBac, DataTable dtNgach)
        {
            using (DataTable dt = dtBac.GetChanges(DataRowState.Deleted))
            {
                if (dt != null)
                    foreach (DataRow dr in dt.GetChanges(DataRowState.Deleted).Rows)
                    {
                        Grade_DeleteDetail(Globals.DB_GetValue<int>(dr["GradeDetail_ID", DataRowVersion.Original]));
                    }
            }

            //
            dtBac.AcceptChanges();
            //
            GradeDetailInfo objInfo;
            foreach (DataRow dr in dtBac.Rows)
            {
                if (!String.IsNullOrEmpty( Globals.Object_GetString(dr["ParentGUID"],"")) ||
                    !String.IsNullOrEmpty( Globals.Object_GetString(dr["GradeDetail_GradeID"],""))
                    )
                {
                    objInfo = new GradeDetailInfo();
                    Globals.DataRow_FillObject(dr, objInfo);
                    //
                    if (objInfo.GradeDetail_GradeId <= 0)
                    {
                        string sFilter = "GradeGUID = '" + dr["ParentGUID"].ToString() + "'";
                        objInfo.GradeDetail_GradeId = Globals.Object_GetValue<int>(dtNgach.Select(sFilter)[0]["Grade_Id"]);
                    }

                    if (objInfo.GradeDetail_Id > 0)
                        Grade_UpdateDetail(objInfo);
                    else
                    {
                        dr["GradeDetail_ID"] =  Grade_InsertDetail(objInfo);
                    }
                }
            }
        }

        public int Grade_InsertDetail(GradeDetailInfo eInfo)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_GradeDetail_Insert", false), 
                eInfo.GradeDetail_GradeId,
                eInfo.GradeDetail_LevelId,
                eInfo.GradeDetail_LevelName,
                eInfo.GradeDetail_Amount,
                eInfo.GradeDetail_BranchCode,
                eInfo.GradeDetail_Month
            ));
        }

        public bool Grade_UpdateDetail(GradeDetailInfo eInfo)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_GradeDetail_Update", false), 
                eInfo.GradeDetail_Id,
                eInfo.GradeDetail_GradeId,
                eInfo.GradeDetail_LevelId,
                eInfo.GradeDetail_LevelName,
                eInfo.GradeDetail_Amount,
                eInfo.GradeDetail_BranchCode,
                eInfo.GradeDetail_Month
            ));
        }

        public bool Grade_DeleteDetail(int GradeDetailID)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_GradeDetail_Delete", false), 
                GradeDetailID));
        }
    }
}
